/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.liu.modules.business.service.impl;

import com.liu.modules.business.domain.NovelDetailInfo;
import com.liu.modules.business.domain.NovelInfo;
import com.liu.modules.business.repository.NovelDetailInfoRepository;
import com.liu.utils.*;
import lombok.RequiredArgsConstructor;
import com.liu.modules.business.repository.NovelInfoRepository;
import com.liu.modules.business.service.NovelInfoService;
import com.liu.modules.business.service.dto.NovelInfoDto;
import com.liu.modules.business.service.dto.NovelInfoQueryCriteria;
import com.liu.modules.business.service.mapstruct.NovelInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author Liu
* @date 2022-07-03
**/
@Service
@RequiredArgsConstructor
public class NovelInfoServiceImpl implements NovelInfoService {

    private final NovelInfoRepository novelInfoRepository;
    private final NovelInfoMapper novelInfoMapper;
    private final NovelDetailInfoRepository novelDetailInfoRepository;

    @Override
    public Map<String,Object> queryAll(NovelInfoQueryCriteria criteria, Pageable pageable){
        Page<NovelInfo> page = novelInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(novelInfoMapper::toDto));
    }

    @Override
    public List<NovelInfoDto> queryAll(NovelInfoQueryCriteria criteria){
        return novelInfoMapper.toDto(novelInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public NovelInfoDto findById(String id) {
        NovelInfo novelInfo = novelInfoRepository.findById(id).orElseGet(NovelInfo::new);
        ValidationUtil.isNull(novelInfo.getId(),"NovelInfo","id",id);
        return novelInfoMapper.toDto(novelInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public NovelInfoDto create(NovelInfo resources) {
        resources.setId(IdUtil.simpleUUID()); 
        return novelInfoMapper.toDto(novelInfoRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(NovelInfo resources) {
        NovelInfo novelInfo = novelInfoRepository.findById(resources.getId()).orElseGet(NovelInfo::new);
        ValidationUtil.isNull( novelInfo.getId(),"NovelInfo","id",resources.getId());
        novelInfo.copy(resources);
        novelInfoRepository.save(novelInfo);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            novelInfoRepository.deleteById(id);
            //删除小说章节
            NovelDetailInfo novelDetailInfo = new NovelDetailInfo();
            novelDetailInfo.setNovelId(id);
            novelDetailInfoRepository.delete(novelDetailInfo);
        }

    }

    @Override
    public void download(List<NovelInfoDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (NovelInfoDto novelInfo : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("类别", novelInfo.getFenleiId());
            map.put("小说图片",novelInfo.getNovelPicUrl());
            map.put("名称", novelInfo.getNovelName());
            map.put("作者", novelInfo.getNovelAuthor());
            map.put("类型", novelInfo.getNovelType());
            map.put("状态", novelInfo.getNovelState());
            map.put("最新章节", novelInfo.getNewChapter());
            map.put("简介", novelInfo.getNovelIntro());
            map.put("详情", novelInfo.getNovelUrl());
            map.put("目录", novelInfo.getNovelDetailUrl());
            map.put("创建时间", novelInfo.getCreateTime());
            map.put("更新时间", novelInfo.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<Map<String, String>> queryNovels() {
        return novelInfoRepository.queryNovels();
    }
}